// pages/test/values.js
let testCards = require('../../data/testCards.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentStep: 0,
    currentStepA: -1,
    currentCard: {},
    currentCardIndex: 0,
    cards: [],
    result: [],
    selectedCards: [],
    showModal: false,
    removeReason: '',
    selectedCardsOnB: [],
    selectedCardsOnA1: [],
    selectedCardsOnA1Reason: '',
    selectedCardsOnA2: [],
    selectedCardsOnA2Reason: '',
    selectedCardsOnA3: [],
    selectedCardsOnA3Reason: '',
    removeLogs: [],
    removeActionLogs: [0, 0, 0]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var cards = testCards.values
    var cardsLength = cards.length
    var sorts = ['A', 'B', 'C', 'D']
    // for (var i = 0; i < cardsLength; i++) {
    //   cards[i].idx = i
    //   cards[i].sort = sorts[i % 4]
    // }
    //cards[0].sort = 'B'
    //cards[8].sort = 'B'
    //cards[24].sort = 'B'
    this.setData({
      cards: cards,
      currentCard: cards[0]
    })
    var result = [0, 0, 0, 0]
    var selectedCards = []
    for (i = 0; i < this.data.cards.length; i++) {
      var card = this.data.cards[i]
      if (card.sort === 'A') {
        result[0]++
        selectedCards.push(card)
      } else if (card.sort === 'B') {
        result[1]++
      } else if (card.sort === 'C') {
        result[2]++
      } else {
        result[3]++
      }
    }
    this.setData({
      result: result,
      selectedCards: selectedCards
    })
    //this.toStep1()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onBtnTap: function (event) {
    var dtype = event.currentTarget.dataset.type
    this.data.cards[this.data.currentCardIndex].sort = dtype
    var card = this.data.cards[this.data.currentCardIndex]
    this.setData({
      cards: this.data.cards
    })
    if (this.data.currentCardIndex === this.data.cards.length - 1) {
      this.toStep1()
      return
    }
    this.nextCard()
  },
  onPreTap: function () {
    if (this.data.currentCardIndex === 0) {
      wx.showToast({
        title: '前面没有卡片额',
        icon: 'none'
      })
      return
    }
    this.preCard()
  },
  onNextTap: function () {
    var currentCard = this.data.cards[this.data.currentCardIndex]
    if (!currentCard.sort) {
      wx.showToast({
        title: '当前卡片还没有分配，请选择',
        icon: 'none'
      })
      return
    }
    var nextCardIndex = this.data.currentCardIndex + 1
    if (nextCardIndex >= this.data.cards.length) {
      wx.showToast({
        title: '后面没有卡片了额',
        icon: 'none'
      })
      return
    }
    this.nextCard()
  },
  preCard: function () {
    if (this.data.currentCardIndex === 0) {
      return
    }
    var preCardIndex = this.data.currentCardIndex - 1
    if (preCardIndex < this.data.cards.length) {
      this.setData({
        currentCardIndex: preCardIndex,
        currentCard: this.data.cards[preCardIndex]
      })
    }
  },
  nextCard: function () {
    var nextCardIndex = this.data.currentCardIndex + 1
    if (nextCardIndex < this.data.cards.length) {
      this.setData({
        currentCardIndex: nextCardIndex,
        currentCard: this.data.cards[nextCardIndex]
      })
    }
  },
  toStep1: function () {
    var result = [0, 0, 0, 0]
    for (var i = 0; i < this.data.cards.length; i++) {
      var card = this.data.cards[i]
      if (card.sort === 'A') {
        result[0]++
      } else if (card.sort === 'B') {
        result[1]++
      } else if (card.sort === 'C') {
        result[2]++
      } else {
        result[3]++
      }
    }
    if (result[0] < 8) {
      this.setData({
        result: result,
        currentStep: 131,
      })
      wx.showModal({
        content: '由于非常重视的卡片不足8张，需从比较重视里选' + (8 - result[0]) + '张',
        showCancel: false
      })
    } else {
      this.setData({
        result: result,
        currentStep: 1,
        currentStepA: 0
      })
    }
  },
  showResult: function (event) {
    this.setData({
      showCards: []
    })
    var dtype = event.currentTarget.dataset.type
    var dname = event.currentTarget.dataset.name
    var cardsLength = this.data.cards.length
    var showCards = []
    for (var i = 0; i < cardsLength; i++) {
      var card = this.data.cards[i]
      if (card.sort === dtype) {
        showCards.push(card)
      }
    }
    this.setData({
      showCards: showCards,
      showResult: true,
      showResultTitle: dname
    })
  },
  onHideResultTap: function (event) {
    this.setData({
      showResult: false,
      showResultTitle: "结果显示"
    })
  },
  onCardTap: function (event) {
    var index = event.currentTarget.dataset.index
    var selectedCards = this.data.selectedCards
    var card = selectedCards[index]
    var that = this
    if (selectedCards.length > 8) {
      wx.showModal({
        title: '确定剔除该卡片？',
        content: card.name,
        success: function (res) {
          if (res.confirm) {
            that.data.cards[card.idx].sort = 'B'
            selectedCards.splice(index, 1)
            that.setData({
              selectedCards: selectedCards,
              cards: that.data.cards
            })
          } else if (res.cancel) {
          }
        }
      })
    } else {
      var idx = event.currentTarget.dataset.idx
      var currentRemoveStep = this.getCurrentRemoveStep()
      if (currentRemoveStep === 0) {
        var selectedCardsOnA1 = this.data.selectedCardsOnA1
        if (selectedCardsOnA1.includes(index)) {
          selectedCardsOnA1.splice(selectedCardsOnA1.indexOf(index), 1)
          selectedCards[index].selectedOnA = false
        } else {
          if (selectedCardsOnA1.length >= 2) {
            wx.showModal({
              content: '最多选择2张，点已选的可以取消选择，再做重新选取',
              showCancel: false
            })
            return
          }
          selectedCardsOnA1.push(index)
          selectedCards[index].selectedOnA = true
        }
        this.setData({
          selectedCards: selectedCards,
          selectedCardsOnA1: selectedCardsOnA1,
        })
      } else if (currentRemoveStep === 1) {
        var selectedCardsOnA2 = this.data.selectedCardsOnA2
        if (selectedCardsOnA2.includes(index)) {
          selectedCardsOnA2.splice(selectedCardsOnA2.indexOf(index), 1)
          selectedCards[index].selectedOnA = false
        } else {
          if (selectedCardsOnA2.length >= 2) {
            wx.showModal({
              content: '最多选择2张，点已选的可以取消选择，再做重新选取',
              showCancel: false
            })
            return
          }
          selectedCardsOnA2.push(index)
          selectedCards[index].selectedOnA = true
        }
        this.setData({
          selectedCards: selectedCards,
          selectedCardsOnA2: selectedCardsOnA2,
        })
      
      } else if (currentRemoveStep === 2) { 
        var selectedCardsOnA3 = this.data.selectedCardsOnA3
        if (selectedCardsOnA3.includes(index)) {
          selectedCardsOnA3.splice(selectedCardsOnA3.indexOf(index), 1)
          selectedCards[index].selectedOnA = false
        } else {
          if (selectedCardsOnA3.length >= 1) {
            wx.showModal({
              content: '最多选择1张，点已选的可以取消选择，再做重新选取',
              showCancel: false
            })
            return
          }
          selectedCardsOnA3.push(index)
          selectedCards[index].selectedOnA = true
        }
        this.setData({
          selectedCards: selectedCards,
          selectedCardsOnA3: selectedCardsOnA3,
        })
      }
    }


  },
  onModalCancelTap: function () {
    console.log("用户点击了取消")
    this.setData({
      showModal: false
    })
  },
  onModalConfirmTap: function () {
    var currentRemoveStep = this.getCurrentRemoveStep()
    this.modalConfirmCallback = () => {
      if (this.data.removeActionLogs[currentRemoveStep] != 0) {
        return
      }
      this.data.removeActionLogs[currentRemoveStep] = 1
      if (currentRemoveStep === 0) {
        var newSelectedCardsIdx = []
        for (var i = 0; i < this.data.selectedCardsOnA1.length; i++) {
          var removeIndex = this.data.selectedCardsOnA1[i]
          if (i === 1 && removeIndex > this.data.selectedCardsOnA1[0]) {
            removeIndex = removeIndex - 1
          }
          var removeCard = this.data.selectedCards.splice(removeIndex, 1)
          var removeCardIdx = removeCard[0].idx
          newSelectedCardsIdx.push(removeCardIdx)
        }
        this.setData({
          selectedCards: this.data.selectedCards,
          removeActionLogs: this.data.removeActionLogs,
          selectedCardsOnA1Reason: this.data.removeReason,
          selectedCardsOnA1: newSelectedCardsIdx,
          removeReason: '',
          showModal: false,
          currentStepA: 1          
        })
        this.toPageTop()
        console.log('剔除第一步：')
        for (var i = 0; i < this.data.selectedCardsOnA1.length; i++) {
          var card = this.data.cards[this.data.selectedCardsOnA1[i]]
          console.log('剔除了：' + card.name)
        }
        console.log('剔除理由：' + this.data.selectedCardsOnA1Reason)

      } else if (currentRemoveStep === 1) {
        for (var i = 0; i < this.data.selectedCardsOnA2.length; i++) {
          var removeIndex = this.data.selectedCardsOnA2[i]
          if (i === 1 && removeIndex > this.data.selectedCardsOnA2[0]) {
            removeIndex = removeIndex - 1
          }
          this.data.selectedCards.splice(removeIndex, 1)
        }
        this.setData({
          selectedCards: this.data.selectedCards,
          removeActionLogs: this.data.removeActionLogs,
          selectedCardsOnA2Reason: this.data.removeReason,
          removeReason: '',
          showModal: false,
          currentStepA: 2          
        })
        this.toPageTop()
        console.log('剔除第二步：')
        for (var i = 0; i < this.data.selectedCardsOnA2.length; i++) {
          var card = this.data.cards[this.data.selectedCardsOnA2[i]]
          console.log('剔除了：' + card.name)
        }
        console.log('剔除理由：' + this.data.selectedCardsOnA2Reason)

      } else if (currentRemoveStep === 2) {
        for (var i = 0; i < this.data.selectedCardsOnA3.length; i++) {
          var removeIndex = this.data.selectedCardsOnA3[i]
          if (i === 1 && removeIndex > this.data.selectedCardsOnA3[0]) {
            removeIndex = removeIndex - 1
          }
          this.data.selectedCards.splice(removeIndex, 1)
        }
        this.setData({
          selectedCards: this.data.selectedCards,
          removeActionLogs: this.data.removeActionLogs,
          selectedCardsOnA3Reason: this.data.removeReason,
          removeReason: '',
          showModal: false,
          currentStepA: 3          
        })
        this.toPageTop()
        console.log('剔除第三步：')
        for (var i = 0; i < this.data.selectedCardsOnA3.length; i++) {
          var card = this.data.cards[this.data.selectedCardsOnA3[i]]
          console.log('剔除了：' + card.name)
        }
        console.log('剔除理由：' + this.data.selectedCardsOnA3Reason)

      }

    }

    this.modalConfirmCallback()

  },
  onSelectCardTap: function (e) {
    var index = e.currentTarget.dataset.index
    var selectedCardsOnB = this.data.selectedCardsOnB
    var arrIndex = selectedCardsOnB.indexOf(index)
    var cards = this.data.cards
    if (arrIndex >= 0) {
      selectedCardsOnB.splice(arrIndex, 1)
      cards[index].selectedOnB = false
      this.setData({
        selectedCardsOnB: selectedCardsOnB,
        cards: cards
      })
      return
    }
    var leftToSelected = 8 - this.data.selectedCards.length
    if (this.data.selectedCardsOnB.length >= leftToSelected) {
      wx.showModal({
        content: '最多选择' + leftToSelected + '张，点已选的可以取消选择，再做重新选取',
        showCancel: false
      })
      return
    }

    selectedCardsOnB.push(index)
    cards[index].selectedOnB = true
    this.setData({
      selectedCardsOnB: selectedCardsOnB,
      cards: cards
    })
  },
  onSelectCardBTap: function (e) {
    var selectedCardsOnB = this.data.selectedCardsOnB
    var cards = this.data.cards
    var selectedCards = this.data.selectedCards
    for (var i = 0; i < selectedCardsOnB.length; i++) {
      cards[selectedCardsOnB[i]].sort = 'A'
      selectedCards.push(cards[selectedCardsOnB[i]])
    }
    selectedCardsOnB = []
    this.setData({
      cards: cards,
      selectedCards: selectedCards,
      selectedCardsOnB: selectedCardsOnB
    })
    this.toStep1()
  },
  onSelectCardATap: function (e) {
    var isShowModal = this.data.showModal
    if (isShowModal) {

    } else {
      this.setData({
        showModal: true
      })
    }
  },
  onTextInput: function (e) {
    this.setData({
      removeReason: e.detail.value
    })
  },
  getCurrentRemoveStep: function () {
    var currentRemoveStep = -1;
    if (this.data.selectedCards.length == 8) {
      currentRemoveStep = 0
    } else if (this.data.selectedCards.length == 6) {
      currentRemoveStep = 1
    } else if (this.data.selectedCards.length == 4) {
      currentRemoveStep = 2
    }
    return currentRemoveStep
  },
  toPageTop: function() {
    console.log('page scroll')
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 600
    })
  }
})